package com.swust.book_shop.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swust.book_shop.constant.JWTConstant;
import com.swust.book_shop.pojo.*;
import com.swust.book_shop.pojo.vo.BookItemVo;
import com.swust.book_shop.pojo.vo.BookQueryVo;
import com.swust.book_shop.pojo.vo.BookVo;
import com.swust.book_shop.service.BookCategoryService;
import com.swust.book_shop.service.BookImageService;
import com.swust.book_shop.service.BookInfoService;
import com.swust.book_shop.service.UserInfoService;
import com.swust.book_shop.util.OSSUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liushuai
 * @create 2022-05-06 15:41
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookInfoService bookInfoService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    BookImageService bookImageService;
    @Autowired
    BookCategoryService bookCategoryService;

    @ApiOperation("图书上传")
    @PutMapping("/upload")
    public Result upload(@RequestHeader(JWTConstant.AUTH_HEADER)String token,@RequestBody BookVo bookVo){
        JWT jwt = JWTUtil.parseToken(token);
        String account = (String) jwt.getPayload("account");
        try {
            int id = bookInfoService.uploadBook(account, bookVo);
            log.info("图书id:{}",id);
            return Result.ok(id);
        } catch (Exception e) {
            log.error("图书上传失败:{}",e.getMessage());
            return  Result.build(203,e.getMessage());
        }
    }
    @ApiOperation("图书封面上传")
    @PostMapping("/upload/image")
    public  Result uploadImage(MultipartFile file) throws IOException {
        log.info("文件接口进入");
        log.info("文件名:{}",file.getOriginalFilename());
        String s = OSSUtils.uploadFile(file);
        log.info("文件路径{}",s);
        return  Result.ok(s);
    }
    @ApiOperation("保存图书路径")
    @PostMapping("/upload/imagePath")
    public  Result uploadImagePath(@RequestBody BookImage bookImage){
        boolean save = bookImageService.save(bookImage);
        if(save){
            return Result.ok("保存成功");
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("分页查询图书")
    @PostMapping("/query/{page}")
    public  Result queryBooksLimit(@PathVariable("page") Integer currentPage){
        Page<BookItemVo> bookItemVoPage = new Page<>(currentPage,12);
        bookItemVoPage.setTotal(9);
        IPage<BookItemVo> bookItemVoIPage = bookInfoService.queryBookItemLimit(bookItemVoPage);
        return  Result.ok(bookItemVoIPage);
    }

    @ApiOperation("分页带条件查询图书")
    @PostMapping("/query/condition/{page}")
    public  Result queryBookLimitByPriceOrName(@RequestBody @Validated BookQueryVo bookQueryVo,
                                               @PathVariable("page")Integer currentPage){
        Page<BookItemVo> bookItemVoPage = new Page<>(currentPage,12);
        bookItemVoPage.setTotal(9);
        IPage<BookItemVo> bookItemVoIPage = bookInfoService.queryBookItemLimitByPriceOrBookName(bookItemVoPage, bookQueryVo);
        return Result.ok(bookItemVoIPage);
    }

    @ApiOperation("获取图书种类")
    @GetMapping("/query/category")
    public  Result  getCategory(){
        List<BookCategory> list = bookCategoryService.list();
        List<String> categoryNameList = new ArrayList<>();
        list.stream().forEach(item->{
            categoryNameList.add(item.getCategory());
        });
        return Result.ok(categoryNameList);
    }
    @ApiOperation("根据图书种类获取图书")
    @GetMapping("/query/{currentPage}/{category}")
    public  Result queryBookByCategory(@PathVariable("category") String category,
                                        @PathVariable("currentPage") Integer currentPage  ){
        Page<BookItemVo> bookItemVoPage = new Page<>(currentPage,12);
        IPage<BookItemVo> bookItemVoIPage = bookInfoService.queryBookByCategory(bookItemVoPage, category);
        return  Result.ok(bookItemVoIPage);
    }
}
